﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CmsContentOwner
    {
        public int Add(KeRui.Model.CmsContentOwner model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@UserName", SqlDbType.NVarChar, 20), new SqlParameter("@SellerID", SqlDbType.Int, 4), new SqlParameter("@SellerName", SqlDbType.NVarChar, 50), new SqlParameter("@ContentID", SqlDbType.Int, 4), new SqlParameter("@ContentTitle", SqlDbType.NVarChar, 200), new SqlParameter("@OwnerType", SqlDbType.Int, 4), new SqlParameter("@ModelID", SqlDbType.Int, 4), new SqlParameter("@PlusID", SqlDbType.Int, 4), new SqlParameter("@PlusName", SqlDbType.VarChar, 50) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.UserID;
            parameters[2].Value = model.UserName;
            parameters[3].Value = model.SellerID;
            parameters[4].Value = model.SellerName;
            parameters[5].Value = model.ContentID;
            parameters[6].Value = model.ContentTitle;
            parameters[7].Value = model.OwnerType;
            parameters[8].Value = model.ModelID;
            parameters[9].Value = model.PlusID;
            parameters[10].Value = model.PlusName;
            DbHelperSQL.RunProcedure("CmsContentOwner_ADD", parameters, out num);
            return (int) parameters[0].Value;
        }

        public int Delete(int ID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = ID;
            return DbHelperSQL.RunProcedure("CmsContentOwner_Delete", parameters, out num);
        }

        public KeRui.Model.CmsContentOwner GetModel(int ID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = ID;
            KeRui.Model.CmsContentOwner owner = new KeRui.Model.CmsContentOwner();
            DataSet set = DbHelperSQL.RunProcedure("CmsContentOwner_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    owner.ID = int.Parse(set.Tables[0].Rows[0]["ID"].ToString());
                }
                if (set.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    owner.UserID = int.Parse(set.Tables[0].Rows[0]["UserID"].ToString());
                }
                owner.UserName = set.Tables[0].Rows[0]["UserName"].ToString();
                if (set.Tables[0].Rows[0]["SellerID"].ToString() != "")
                {
                    owner.SellerID = int.Parse(set.Tables[0].Rows[0]["SellerID"].ToString());
                }
                owner.SellerName = set.Tables[0].Rows[0]["SellerName"].ToString();
                if (set.Tables[0].Rows[0]["ContentID"].ToString() != "")
                {
                    owner.ContentID = int.Parse(set.Tables[0].Rows[0]["ContentID"].ToString());
                }
                owner.ContentTitle = set.Tables[0].Rows[0]["ContentTitle"].ToString();
                if (set.Tables[0].Rows[0]["OwnerType"].ToString() != "")
                {
                    owner.OwnerType = int.Parse(set.Tables[0].Rows[0]["OwnerType"].ToString());
                }
                if (set.Tables[0].Rows[0]["ModelID"].ToString() != "")
                {
                    owner.ModelID = int.Parse(set.Tables[0].Rows[0]["ModelID"].ToString());
                }
                if (set.Tables[0].Rows[0]["Cost"].ToString() != "")
                {
                    owner.Cost = int.Parse(set.Tables[0].Rows[0]["Cost"].ToString());
                }
                if (set.Tables[0].Rows[0]["Status"].ToString() != "")
                {
                    owner.Status = int.Parse(set.Tables[0].Rows[0]["Status"].ToString());
                }
                if (set.Tables[0].Rows[0]["PlusID"].ToString() != "")
                {
                    owner.PlusID = int.Parse(set.Tables[0].Rows[0]["PlusID"].ToString());
                }
                owner.PlusName = set.Tables[0].Rows[0]["PlusName"].ToString();
                return owner;
            }
            return null;
        }
    }
}

